/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-21
 * 修改历史 :
 * 1. [2016-11-21]创建文件 by chenxin
 */
package com.sinosoft.sinopay.service.customer.entry.facade;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.util.DateUtil;
import com.sinosoft.sinocloud.platform.common.util.PersonIDUtil;
import com.sinosoft.sinopay.api.common.dto.pay.PayAccountInfoDTO;
import com.sinosoft.sinopay.customer.dto.CustomerInfoDTO;
import com.sinosoft.sinopay.customer.facade.CustomerFacadeService;
import com.sinosoft.sinopay.service.customer.biz.service.CustomerBankAcountCoreService;
import com.sinosoft.sinopay.service.customer.biz.service.CustomerInfoCoreService;

/**
 * 【客户微服务实现类】
 * @author chenxin
 * @date 2016-11-21 下午5:37 
 * @version V1.0
 */
@Service
public class CustomerFacadeServiceImpl extends SinoBaseService implements CustomerFacadeService {
    @Autowired
    private CustomerBankAcountCoreService bankAccountService;
    @Autowired
    private CustomerInfoCoreService customerService;

    @Override
    public Integer saveCustomerPayAccountInfo(PayAccountInfoDTO payInfoDTO) {
        CustomerInfoDTO tCustomerDTO = new CustomerInfoDTO();
        //根据证件号，获取客户5要素信息
        String birthday = null;
        String gender = null;
        String IdType = payInfoDTO.getCardType();
        if(IdType!=null && IdType.equals("1"))//1-身份证
        {
        	birthday = PersonIDUtil.getBirthByIdCard(payInfoDTO.getCardNo());
            gender = PersonIDUtil.getGenderByIdCard(payInfoDTO.getCardNo());
            tCustomerDTO.setBirthday(DateUtil.parseDate(birthday,"yyyyMMdd"));
        }
        tCustomerDTO.setIdType(IdType);
        tCustomerDTO.setCustomerName(payInfoDTO.getAccountName());
        tCustomerDTO.setGender(gender);
        tCustomerDTO.setIdType(payInfoDTO.getCardType());
        tCustomerDTO.setIdNo(payInfoDTO.getCardNo());
        tCustomerDTO.setMobile(payInfoDTO.getMobile());
        Integer customerId = customerService.checkCustomerExist(tCustomerDTO);
        Integer customerfalg = null ;
        tCustomerDTO.setCustomerId(customerId);
        if(customerId!=null && customerId != 0){
        	bankAccountService.modifyBankAcountForCustomer(customerId, payInfoDTO);
        	customerfalg=1;
		}else{
			customerService.addNewCustomer(tCustomerDTO);
			Integer cusId = customerService.checkCustomerExist(tCustomerDTO);
			bankAccountService.addNewBankAcountForCustomer(tCustomerDTO.getCustomerId(), payInfoDTO);
			customerfalg=0;
		}
		return customerfalg;
    }
}
